package wykop.main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.hibernate.Session;

import wykop.analyze.AnalyzeConfiguration;
import wykop.analyze.CreateArffFile;
import wykop.dataBase.UserManager;
import wykop.download.PageDownloader;
import wykop.download.process.PopulateDatabaseConf;
import wykop.download.process.UpdateDatabase;
import wykop.main.configuration.Configuration;
import wykop.page.WykopId;
import wykop.page.WykopPage;
import wykop.parser.HibernateUtil;

public class Main {

	private static void configureLogger() throws IOException {
		// redirect logging to local file
		BasicConfigurator.configure(new FileAppender(new PatternLayout(
				"%d{dd MMM yyyy HH:mm:ss,SSS} %r [%t] %-5p %c - %m%n"),
				"wykop.parser.log"));
		// turn off hibernate log (it is too big)
		Logger.getLogger("org.hibernate").setLevel(Level.WARN);
	}

	public static void main(final String[] args) throws InterruptedException,
			IOException {

		configureLogger();
		populateDatabase();
		startLogging();
	}
	
	private static void populateDatabase()
	{
		PopulateDatabaseConf populateDatabaseConf = Configuration.fromFile(PopulateDatabaseConf.class, "WykopParser.ini");
		UpdateDatabase populateDatabase = new UpdateDatabase(populateDatabaseConf, HibernateUtil.getSessionFactory() );
		new Thread(  populateDatabase ).start();
	}

	private static void startLogging()
	{
		AnalyzeConfiguration ac  = Configuration.fromFile(AnalyzeConfiguration.class, "WykopParser.ini");
		new Thread( CreateArffFile.startLogging(ac) ).start();
	}
	
}